package com.hotel.repository;

import com.hotel.config.DatabaseConfig;
import com.hotel.entity.Consumption;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ConsumptionRepository {
    public ConsumptionRepository() {}

    public void save(Consumption consumption) {
        String sql = "insert into consumptions (checkin_id, item_id, quantity)values(?,?,?)";
        try (Connection conn = DriverManager.getConnection(DatabaseConfig.URL, DatabaseConfig.USER, DatabaseConfig.PASSWORD);
             PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
            stmt.setInt(1, consumption.getCheckin_id());
            stmt.setInt(2, consumption.getItem_id());
            stmt.setInt(3, consumption.getQuantity());
            stmt.executeUpdate();
            try (ResultSet rs = stmt.getGeneratedKeys()) {
                if (rs.next()) {
                    consumption.setConsumption_id(rs.getInt(1));
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException("保存消费记录失败", e);
        }
    }

    //通过入住记录查消费情况
    public List<Map<String, Object>> findById(Integer id) {
        String sql = "select consumption_id, item_name,quantity,price, amount, consume_time " +
                "from consumptions  join menuitems on menuitems.item_id = consumptions.item_id where checkin_id = ?";
        List<Map<String, Object>> consumptions = new ArrayList<>();
        try (Connection conn = DriverManager.getConnection(DatabaseConfig.URL, DatabaseConfig.USER, DatabaseConfig.PASSWORD);
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, id);
            try (ResultSet rs = stmt.executeQuery()) {
                while(rs.next()) {
                    Map<String, Object> consumption = new HashMap<>();
                    consumption.put("consumption_id", rs.getInt("consumption_id"));
                    consumption.put("item_name", rs.getString("item_name"));
                    consumption.put("quantity", rs.getInt("quantity"));
                    consumption.put("unit_price", rs.getBigDecimal("price"));
                    consumption.put("amount", rs.getBigDecimal("amount"));
                    consumption.put("consume_time", rs.getTimestamp("consume_time"));
                    consumptions.add(consumption);
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException("查询消费记录失败", e);
        }
        return consumptions;
    }

    public void deleteById(Integer id) {
        String sql = "delete from consumptions where consumption_id = ?";
        try (Connection conn = DriverManager.getConnection(DatabaseConfig.URL, DatabaseConfig.USER, DatabaseConfig.PASSWORD);
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, id);
            stmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException("删除消费记录失败", e);
        }
    }
}
